<?php
App::uses('AppController', 'Controller');
/**
 * PinCodes Controller
 *
 * @property PinCode $PinCode
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class PinCodesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Session');

/**
 * index method
 *
 * @return void
 */
	public function admin_index($id=null) {
                                    if ($this->request->is('post','put')) {
			$this->PinCode->create();
			if ($this->PinCode->save($this->request->data)) {
				$this->Session->setFlash(__('The pin code has been saved.'));
				return $this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('The pin code could not be saved. Please, try again.'));
			}
		}
                                    else if($id)
                                    {
                                                     $options = array('conditions' => array('PinCode.' . $this->PinCode->primaryKey => $id));
			$this->request->data = $this->PinCode->find('first', $options);
                                                       $this->set('edit',1);
                                   
                                    }
		
                                    $this->PinCode->recursive = 0;
                                    $this->loadModel('MrCode');
                                    $mrCode = $this->MrCode->find('list',array('fields'=>array('id','name_code')));
                                    $this->set(compact('mrCode'));
		$this->set('pinCodes', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->PinCode->exists($id)) {
			throw new NotFoundException(__('Invalid pin code'));
		}
		$options = array('conditions' => array('PinCode.' . $this->PinCode->primaryKey => $id));
		$this->set('pinCode', $this->PinCode->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->PinCode->create();
			if ($this->PinCode->save($this->request->data)) {
				$this->Session->setFlash(__('The pin code has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The pin code could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->PinCode->exists($id)) {
			throw new NotFoundException(__('Invalid pin code'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->PinCode->save($this->request->data)) {
				$this->Session->setFlash(__('The pin code has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The pin code could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('PinCode.' . $this->PinCode->primaryKey => $id));
			$this->request->data = $this->PinCode->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->PinCode->id = $id;
		if (!$this->PinCode->exists()) {
			throw new NotFoundException(__('Invalid pin code'));
		}
		$this->request->allowMethod('post', 'delete','get');
		if ($this->PinCode->delete()) {
			$this->Session->setFlash(__('The pin code has been deleted.'));
		} else {
			$this->Session->setFlash(__('The pin code could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'admin_index'));
	}
}